using Admin.Domain.Events;
using Admin.Domain.IEvents;
using Microsoft.Extensions.DependencyInjection;

namespace Admin.Infrastructure.Events;

public class EventPublisher(IServiceProvider serviceProvider) : IEventPublisher
{
    private readonly IServiceProvider _serviceProvider = serviceProvider;

    public async Task Publish<T>(T domainEvent) where T : IDomainEvent
    {
        var handlers = _serviceProvider.GetServices<IEventHandler<T>>();
        foreach (var handler in handlers)
        {
            await handler.Handle(domainEvent);
        }
    }
}